from flask import Blueprint, request, session, jsonify

from app.models.tablemodel import Comment, Credit, Users, Article, Opinion, Notification
import re
from utils.redis_util import redis_client

co_bp = Blueprint('comment', __name__)

# 拦截器没有登录不让评论
@co_bp.before_request
def before_comment():
    if session.get('islogin') is None or session.get('islogin') != 'true':
        return '你还没有登录，不能发表评论'


# 原始评论接口
@co_bp.route('/comment', methods=['POST'])
def add_comment():
    articleid = request.form.get('articleid')
    content = request.form.get('content').strip()
    content = re.sub(r'[ \t]', '', content)  # 剔除空格和制表符
    ipaddr = request.remote_addr

    # 对评论内容进行简单检验
    if len(content) < 2 or len(content) > 200:
        return 'content-invalid'

    comment = Comment()
    if not comment.check_limit_per_10():
        try:
            id = comment.insert_comment(articleid, content, ipaddr)
            # 评论成功后，更新积分明细和剩余积分，及文章回复数量
            Credit().insert_detail(type='添加评论', target=articleid, credit=2)
            Users().update_credit(2)
            Article().update_replycount(articleid)
            # 添加通知
            Notification().insert_notification(type='评论', commentid=id, articleid=articleid)
            # 改变redis中的消息通知值
            userid = session.get('userid')
            key_count = f"notify:{userid}:count:comment_count"
            redis_client.incr(key_count)

            return 'add-pass'
        except:
            return 'add-fail'
    else:
        return 'add-limit'


# 提交评论回复de接口
@co_bp.route('/api/comments/reply', methods=['POST'])
def reply_comment():
    data = request.get_json()
    articleid = data.get('articleid')
    commentid = data.get('commentid')
    content = data.get('content').strip()
    content = re.sub(r'[ \t]', '', content)  # 剔除空格和制表符
    ipaddr = request.remote_addr
    # 对评论内容进行简单检验
    if len(content) < 2 or len(content) > 200:
        return 'content-invalid'
    comment = Comment()
    if not comment.check_limit_per_10():
        try:
            cid = comment.insert_reply(commentid, content, articleid, ipaddr)
            # 回复成功后，发通知，更新积分明细和剩余积分，及文章回复数量
            Notification().insert_notification(type="回复", commentid=cid, articleid=articleid)
            Credit().insert_detail(type='回复评论', target=articleid, credit=2)
            Users().update_credit(2)
            Article().update_replycount(articleid)
            # 改变redis中的消息通知值
            userid = session.get('userid')
            key_count = f"notify:{userid}:count:reply_count"
            redis_client.incr(key_count)

            return 'add-pass'
        except:
            return 'add-fail'
    else:
        return 'add-limit'


# 获得到某一文章所有评论的层级
@co_bp.route('/api/replies', methods=['POST'])
def all_replies():
    articleid = request.form.get('articleid')
    all_comments_replies_list = Comment().get_all_comments_replies_with_user_list(articleid)
    return jsonify(all_comments_replies_list)


# 执行评论的赞成或反对，并返回执行结果和点赞数加减情况
@co_bp.route('/api/like', methods=['POST'])
def get_likes():
    commentid = request.form.get('commentid')
    like = request.form.get('like')
    articleid = request.form.get('articleid')
    ipaddr = request.remote_addr
    result = Opinion().do_agree_oppose(commentid, like, ipaddr)
    # 写入点赞通知
    if int(like) == 1:
        Notification().insert_notification(type='点赞', commentid=commentid, articleid=articleid)
        # 改变redis中的消息通知值
        userid = session.get('userid')
        key_count = f"notify:{userid}:count:like_count"
        redis_client.incr(key_count)
    return result
